package appletFour;

import globalCode.TableHandler;
import globalCode.TableHandlerContainer;
import globalCode.TableHandlerThread;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class RebuildPanel extends JPanel implements TableHandlerContainer{
	TableHandler handler;

	public class InputPanel extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		RebuildPanel applet;
		private JButton computeButton;

		public InputPanel(RebuildPanel ma){
			this.setLayout(new BorderLayout());
			applet = ma;
			computeButton = new JButton();
			computeButton.addMouseListener(new MouseListener(){
				@Override
				public void mouseClicked(MouseEvent arg0) {
					applet.compute();
				}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});

			computeButton.setText("Rebuild Database");
			computeButton.setPreferredSize(new Dimension(50, 20));
			this.add(computeButton, BorderLayout.CENTER);
		}

		public void setButtonText(String string) {
			computeButton.setText(string);
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	InputPanel input;
	OutputPanel output;
	private JProgressBar progress;

	boolean otherRunning = false;
	public void compute() {
		if(otherRunning){
			handler.endAll();
			otherRunning = false;
			progress.setValue(0);
			input.setButtonText("Rebuild Database");
			return;
		}
		handler.rebuildDatabase(this);
		otherRunning = true;
		//other.setDaemon(true);
		input.setButtonText("Cancel");
		//output.display(gcs.getRankings());
	}


	public RebuildPanel(OverallApplet overallApplet, Connection c) {
		this(overallApplet, c, new TableHandler(c));
	}

	public RebuildPanel(OverallApplet overallApplet, Connection c, TableHandler th) {
		this.setSize(610, 460);
		this.setVisible(true);
		JPanel child = new JPanel();
		child.setPreferredSize(new Dimension(600, 450));
		child.setLayout(new BorderLayout());
		input = new InputPanel(this);
		child.add(input, BorderLayout.NORTH);
		progress = new JProgressBar();
		child.add(progress, BorderLayout.SOUTH);
		this.add(child);
		handler = th;
	}

	@Override
	public void thtDisplay(int[][] table) {

	}

	@Override
	public void thtUserFinished() {
		//System.out.println("userfinished");
	}

	@Override
	public void thtGroupFinished(TableHandlerThread tht) {
		System.out.println("groupfinished "+tht.getGroupCon().getGroupID());
		progress.setValue(progress.getValue()+1);
		if(progress.getValue() == progress.getMaximum()){
			handler.endAll();
			otherRunning = false;
			input.setButtonText("Rebuild Database");
		}
	}

	@Override
	public void thtUsersInGroup(int num) {
		//System.out.println("usersingroup "+num);
	}

	@Override
	public void thtGroupsInSet(int num) {
		System.out.println("groupsinset "+num);
		progress.setMaximum(num);
	}

	@Override
	public void thtAbortedGroupThread() {
		// TODO Auto-generated method stub

	}

	@Override
	public void thtAbortedRebuild() {
		progress.setValue(0);
	}

}

